#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File          :    process_data.py
@Contact       :    LJL959@QQ.COM
@License       :    (C)Copyright 2020-2021, Liugroup-NLPR-CASIA
@Modify Time   :    2020/12/9 9:51
@Author        :    LiuJiaolong
@Version       :    1.0
@Description    :    数据预处理
"""

# import lib
import os
import torch
import numpy as np
import scipy.io as sio
from mxnet import nd
# data_dir = 'D:\Study\PycharmProjects\D2L\MyFirstDemoWithFMRIData\data'                        # 数据所在路径
train_data_dir = 'D:\Study\PycharmProjects\D2L\MyFirstDemoWithFMRIData\data/trainData'                        # 数据所在路径
test_data_dir = 'D:\Study\PycharmProjects\D2L\MyFirstDemoWithFMRIData\data/testData'                        # 数据所在路径


def process_data():
    normal_dir = test_data_dir + '/health/'       # 健康人员样本所在位置
    patient_dir = test_data_dir + '/drug/'        # 生病人员样本所在位置
    total_dir = [normal_dir, patient_dir]   # 所有人的样本集合
    data_set = []                           # 存储数据

    # n_or_p negative 阴性，健康人员； positive 阳性，病人
    for n_or_p in range(2):
        list_dir = os.listdir(total_dir[n_or_p])
        for k, file in enumerate(list_dir):                         # 枚举
            mat_path = os.path.join(total_dir[n_or_p], file)        # mat文件的样本路径
            mat_i = sio.loadmat(mat_path)                           # 读取mat
            sample_i = np.array(mat_i['ROISignals'])                # 读取mat中的ROISignals，感兴趣区信号
            sample_i = sample_i.T                                   # 转置
            # 取0~116 和 0~150作为训练数据
            sample_i = np.vstack((sample_i[:116, :150], np.zeros((1, 150))))
            # print(sample_i)
            sample_i[-1, 0] = n_or_p                                # 最后一行最后一列存放标签
            data_set.append(sample_i)
    np.save(test_data_dir + '/drug_health_data', data_set)               # 存储在名为drug_health_data的dataset中


if __name__ == '__main__':
    process_data()



    # 读取处理好的npy文件看一下
    # data_with_label = np.load(data_dir + '/drug_health_data.npy')
    # print(data_with_label.shape)
    # label_temp = data_with_label[0, 116, 0]  # 使用封装的数据
    # label_x = torch.tensor(label_temp, dtype=torch.long)
    # print("label", label_x)
    #
    # data_x = data_with_label[0, 0:116, 0:150]  # 0:150 特征，也就是数据，因为前面的116个结点存放数据，第117个结点存标签，所以0:150全取
    # print("data_x", data_x.shape)
